package tomoto.util;

/**
 * Mutable holder of object.
 * 
 * @author Tomoto Shimizu Washio
 * 
 * @param <T> Object type
 */
public class Holder<T> {
	/**
	 * mutable field to hold the object
	 */
	public T obj;

	/**
	 * Creates an instance with no object held.
	 */
	public Holder() {
		this(null);
	}

	/**
	 * Creates an instance with the specified object held.
	 * 
	 * @param obj object to hold
	 */
	public Holder(T obj) {
		this.obj = obj;
	}

	/**
	 * {@inheritDoc} Returns true if the held object by the specified object
	 * equals to its own.
	 */
	@Override
	public boolean equals(Object other) {
		if (other instanceof Holder) {
			Holder<?> o = (Holder<?>) other;
			return ObjectUtil.equals(obj, o.obj);
		} else {
			return false;
		}
	}

	/**
	 * {@inheritDoc} Returns the hash code of the held object.
	 */
	@Override
	public int hashCode() {
		return ObjectUtil.hashCode(obj);
	}
}
